package com.common.tool;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dao.WeixinUserinfo;
import com.entity.weixin.WeiXinUserInfo;
import com.jfinal.kit.PropKit;

/**
 * 微信菜单
 * 
 * @author mm
 *
 */
public class WeiXinMenuUtil {

    private String accessToken = WeiXinUtil.getAccessToken();

    /**
     * 建立微信菜单
     */
    public void createMenu() {
	String url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + accessToken;
	JSONObject json = subMyMenu();

	String s = HttpRequest.sendPost(url, json.toString());
	System.out.println(s);
    }

    public JSONObject subMyMenu() {
	JSONObject json = new JSONObject();
	List buttonLst = new ArrayList();
	List subbuttonLst = null;
	Map button = null;
	Map subbutton = null;

	button = new HashMap();
	button.put("type", "view");
	button.put("name", "首页");
//        String url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf48b8f9f612266bc&redirect_uri=http%3A%2F%2Fmiaowxtest.ngrok.cc%2Fdistribution%2Fweixin%2Findex&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
        
       String url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx4a2775ee91047dc9&redirect_uri=http://www.yznengjian.com/distribution/weixin/weixinindex&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
	 
	button.put("url",url);
	buttonLst.add(button);
	
	button = new HashMap();
	// button.put("type", "click");
	// button.put("name", "二维码");
	// button.put("key", "V001_MYCARD");
//	url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf48b8f9f612266bc&redirect_uri=http://miaowxtest.ngrok.cc/distribution/weixin/membercard&response_type=code&scope=snsapi_base&state=1#wechat_redirect");
	url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx4a2775ee91047dc9&redirect_uri=http://www.yznengjian.com/distribution/weixin/membercard&response_type=code&scope=snsapi_base&state=1#wechat_redirect";

	button.put("type", "view");
	button.put("name", "二维码");
	button.put("url",url);
	buttonLst.add(button);

	button = new HashMap();
	button.put("type", "click");
	button.put("name", "服务中心");
	button.put("key", "V1001_PRODUCT");

	subbutton = new HashMap();

	subbuttonLst = new ArrayList();
	subbutton.put("type", "click");
	subbutton.put("name", "产品介绍");
	subbutton.put("key", "V1001_001_BUY");
	subbuttonLst.add(subbutton);

	subbutton = new HashMap();
	subbutton.put("type", "click");
	subbutton.put("name", "型号参考");
	subbutton.put("key", "V1001_002_SEND");
	subbuttonLst.add(subbutton);

	subbutton = new HashMap();
	// subbutton.put("type", "click");
	// subbutton.put("name", "个人中心");
	// subbutton.put("key", "V1001_MYINFO_SEND");

	
//	url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf48b8f9f612266bc&redirect_uri=http://miaowxtest.ngrok.cc/distribution/weixin/members&response_type=code&scope=snsapi_base&state=1#wechat_redirect");
	
	url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx4a2775ee91047dc9&redirect_uri=http://www.yznengjian.com/distribution/weixin/members&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
	
	
	
	subbutton.put("type", "view");
	subbutton.put("name", "个人中心");
	subbutton.put("url",url);
	// http://localhost:8080/distribution/weixin/members
	subbuttonLst.add(subbutton);

	button.put("sub_button", subbuttonLst);
	buttonLst.add(button);

	// button = new HashMap();
	// button.put("type", "click");
	// button.put("name", "我");
	// button.put("key", "V1001_ME");
	// buttonLst.add(button);

	json.put("button", buttonLst);

	System.out.println(json.toString());
	return json;
    }

    public JSONObject subMenu() {
	JSONObject json = new JSONObject();
	List buttonLst = new ArrayList();
	List subbuttonLst = new ArrayList();
	Map button = null;
	Map subbutton = null;

	button = new HashMap();
	button.put("name", "信息资讯");

	subbutton = new HashMap();
	subbuttonLst = new ArrayList();

	subbutton.put("type", "click");
	subbutton.put("name", "要闻资讯");
	subbutton.put("key", "V105");
	subbuttonLst.add(subbutton);

	subbutton = new HashMap();
	subbutton.put("type", "click");
	subbutton.put("name", "组织工作");
	subbutton.put("key", "V104");
	subbuttonLst.add(subbutton);

	subbutton = new HashMap();
	subbutton.put("type", "click");
	subbutton.put("name", "基层党建");
	subbutton.put("key", "V103");
	subbuttonLst.add(subbutton);

	subbutton = new HashMap();
	subbutton.put("type", "click");
	subbutton.put("name", "干部工作");
	subbutton.put("key", "V102");
	subbuttonLst.add(subbutton);

	subbutton = new HashMap();
	subbutton.put("type", "click");
	subbutton.put("name", "人才工作");
	subbutton.put("key", "V101");
	subbuttonLst.add(subbutton);

	button.put("sub_button", subbuttonLst);
	buttonLst.add(button);

	button = new HashMap();
	button.put("type", "click");
	button.put("name", "专题活动");
	subbutton = new HashMap();
	subbuttonLst = new ArrayList();

	subbutton.put("type", "click");
	subbutton.put("name", "驻村干部");
	subbutton.put("key", "V201");
	subbuttonLst.add(subbutton);

	subbutton = new HashMap();
	subbutton.put("type", "click");
	subbutton.put("name", "两学一做");
	subbutton.put("key", "V202");
	subbuttonLst.add(subbutton);

	subbutton = new HashMap();
	subbutton.put("type", "view");
	subbutton.put("name", "问卷调查");
	subbutton.put("url",
		"http://www.gznw.gov.cn/meta/welcomeTovote.jx?fireName=20161227dczt1&phone=1&name=ycjygzdc");
	subbuttonLst.add(subbutton);

	button.put("sub_button", subbuttonLst);
	buttonLst.add(button);

	button = new HashMap();
	button.put("type", "click");
	button.put("name", "微党建");
	button.put("key", "V301");
	buttonLst.add(button);

	json.put("button", buttonLst);
	System.out.println(json.toString());
	return json;
    }

    /**
     * 删除微信菜单
     */
    public void deleteMenu() {
	String url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" + accessToken;
	String s = HttpRequest.sendGet(url);
	System.out.println(s);
    }

    public JSONObject textMessage(String openId) {
	JSONObject json = new JSONObject();

	json.put("touser", openId);
	json.put("msgtype", "text");

	List texts = new ArrayList();
	Map content = new HashMap();
	content.put("content", "XXXXX申请成为商铺");
	texts.add(content);

	json.put("text", texts);
	System.out.println(json.toString());
	return json;
    }


    
}
